package com.gs.SynPatrolRep.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gs.SynPatrolRep.R;

public class ToolBarTitle extends LinearLayout {
    private TextView item_title;
    private ImageView item_back;
    private TextView item_per;
    private OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        if (onClickListener != null) {
            item_back.setOnClickListener(onClickListener);
            item_per.setOnClickListener(onClickListener);
        }
    }

    public String getTitle() {
        return item_title.getText().toString();
    }

    public void setTitle(String title) {
        item_title.setText(title);
    }

    public ToolBarTitle(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ToolBarTitle);
        LayoutInflater.from(context).inflate(R.layout.tool_bar_title, this, true);
        item_title = findViewById(R.id.item_title);
        item_per = findViewById(R.id.item_per);
        item_back = findViewById(R.id.item_back);
        String text = attributes.getString(R.styleable.ToolBarTitle_tool_bar_title_text);
        item_title.setText(text);
        text = attributes.getString(R.styleable.ToolBarTitle_tool_bar_title_per_text);
        item_per.setText(text);
        String titleVisibility = attributes.getString(R.styleable.ToolBarTitle_tool_bar_title_visibility);
        if (titleVisibility != null && !TextUtils.isEmpty(titleVisibility)) {
            switch (titleVisibility) {
                case "visible":
                    item_title.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_title.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_title.setVisibility(GONE);
                    break;
            }
        }
        String perVisibility = attributes.getString(R.styleable.ToolBarTitle_tool_bar_title_per_visibility);
        if (perVisibility != null && !TextUtils.isEmpty(perVisibility)) {
            switch (perVisibility) {
                case "visible":
                    item_per.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_per.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_per.setVisibility(GONE);
                    break;
            }
        }
        String backVisibility = attributes.getString(R.styleable.ToolBarTitle_tool_bar_title_back_visibility);
        if (backVisibility != null && !TextUtils.isEmpty(backVisibility)) {
            switch (backVisibility) {
                case "visible":
                    item_back.setVisibility(VISIBLE);
                    break;
                case "invisible":
                    item_back.setVisibility(INVISIBLE);
                    break;
                case "gone":
                    item_back.setVisibility(GONE);
                    break;
            }
        }
        attributes.recycle();
    }
}
